﻿# -*- coding:utf-8 -*-
import datetime
import os
import random
import re

import pika


class Demo(object):

    def get_data(self):
        return '{ 	"transactionHead": {	"comments": "2204250000951291",	"employeeId": "1",	"memberCode": "",	"originalPosId": "",	"originalStoreId": "",	"originalTaId": "",	"posId": "0996",	"storeId": "1026",	"taDate": "2022-04-25 16:35:12.0","taId": "220425252408444000","channel": "13",	"taType": "0",	"taxAmount": "",	"totalAmount": "",	"totalInvoiceAmount": "",	"totalAmountWithoutTax": "",	"orderPackingFeeAmount": "0.50",	"returnResult": "" 	}, 	"transactionItem": [	{	"itemName": "咖啡机红色",	"discountFlag": "0",	"unit": "",	"specifications": "",	"quantity": "3.0",	"price": "0.33",	"taxSign": "1",	"itemTaxCode": "",	"itemCode": "10000026",	"amount": "",	"taxRate": "",	"taxAmount": "",	"comments": "2204250000951291",	"freightAmount": "0.0",	"freightAmountWithoutTax": "",	"freightTaxAmount": "",	"couponAmount": "0.0",	"couponAmountWithoutTax": "",	"couponTaxAmount": "",	"promotionAmount": "0.0",	"promotionAmountWithoutTax": "",	"promotionTaxAmount": "",	"amountWithoutTax": "",	"lineTotalAmount": "",	"lineTotalTaxAmount": "",	"lineTotalAmountWithoutTax": "",	"packingFeeAmount": "0",	"packingFeeAmountWithoutTax": "",	"packingFeeTaxAmount": "",	"pointsAmount": "0.0",	"pointsTaxAmount": "",	"pointsAmountWithoutTax": "",	"memberDiscount": "0.0",	"memberDiscountWithoutTax": "",	"memberDiscountTax": "",	"PostageAmount": "0.0",	"PostageAmountWithoutTax": "",	"PostageTaxAmount": "",	"giftcardAmount": "0.0",	"giftcardpackingAmount": "0.0",	"giftcardpostageAmount": "0.0"	} 	] }'

    def change_data(self, index):
        # index = random.randint(100000000000000000, 999999999999999999)
        index = str(index).rjust(10, "0")
        taId = re.findall(r'"taId": "(.*?)","channel": "13"', self.get_data())[0]
        new_str = self.get_data().replace(taId, str(index))
        print(index)
        return new_str
        # f = open(rf'D:\Disktop\mq\{index}.xml', 'w', encoding='utf8')
        # f.write(new_str)
        # f.close()

    def send_mq(self, message):
        connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.224.24.107', port=5672, credentials=pika.PlainCredentials('admin', 'aldi123456')))
        channel = connection.channel()
        channel.queue_declare(queue='mq.midpat.to.ims', durable=True)
        channel.basic_publish(exchange='', routing_key='mq.midpat.to.ims', body=message)
        connection.close()


if __name__ == '__main__':
    demo = Demo()
    for (index, i) in enumerate(range(1000)):
        print(datetime.datetime.today().strftime("%Y%m%D-%H:%M:%S") + "----------" + str(index))
        new_str = demo.change_data(index)
        demo.send_mq(new_str)